#include <jni.h>
#include "ar_uba_fi_taller2_ftrs_index_IndexadorImp.h"
#include "Indexador.h"
#include <iostream>

/******************/
/* JLongToPointer */
/******************/
static void *JLongToPointer(
  jlong value)
  {
   return (void *) value;
  }

/******************/
/* PointerToJLong */
/******************/
static jlong PointerToJLong(
  void *value)
  {
   return (jlong) value;
  }

/*
 * Class:     ar_uba_fi_taller2_ftrs_index_IndexadorImp
 * Method:    create
 * Signature: ()J
 */
JNIEXPORT jlong JNICALL Java_ar_uba_fi_taller2_ftrs_index_IndexadorImp_create__Ljava_lang_String_2Z
  (JNIEnv *env, jobject obj, jstring dataPath, jboolean mode) {

	bool _mode;
	Indexador *index=0;

	if (mode == JNI_TRUE)
		_mode = true;
	else
		_mode = false;

	index = new Indexador( env->GetStringUTFChars(dataPath, 0), env, obj, _mode );
	return (PointerToJLong((void*)index));
}

/*
 * Class:     ar_uba_fi_taller2_ftrs_index_IndexadorImp
 * Method:    create
 * Signature: (I)J
 */
JNIEXPORT jlong JNICALL Java_ar_uba_fi_taller2_ftrs_index_IndexadorImp_create__Ljava_lang_String_2I
  (JNIEnv *env, jobject obj, jstring dataPath, jint cant) {
  	
	Indexador *index=0;
	index = new Indexador( env->GetStringUTFChars(dataPath, 0), (int)cant, env, obj );
	return (PointerToJLong((void*)index));
}

/*
 * Class:     ar_uba_fi_taller2_ftrs_index_IndexadorImp
 * Method:    destroy
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_ar_uba_fi_taller2_ftrs_index_IndexadorImp_destroy
  (JNIEnv *env, jobject obj, jlong i) {
  	
	Indexador *index = (Indexador*)JLongToPointer(i);
	if (index != 0)
		delete index;
	return;
}

/*
 * Class:     ar_uba_fi_taller2_ftrs_index_IndexadorImp
 * Method:    generarIndice
 * Signature: (J)V
 */
JNIEXPORT void JNICALL Java_ar_uba_fi_taller2_ftrs_index_IndexadorImp_generarIndice
  (JNIEnv *env, jobject obj, jlong i) {
  	
	Indexador *index = (Indexador*)JLongToPointer(i);
	if ( index != 0)
		index->generarIndice();
}

/*
 * Class:     ar_uba_fi_taller2_ftrs_index_IndexadorImp
 * Method:    nuevoDocumento
 * Signature: (Ljava/lang/String;J)V
 */
JNIEXPORT void JNICALL Java_ar_uba_fi_taller2_ftrs_index_IndexadorImp_nuevoDocumento
  (JNIEnv *env, jobject obj , jstring nombre, jlong i) {
  	
	const char *name = (env)->GetStringUTFChars(nombre,0);
	Indexador *index = (Indexador*)JLongToPointer(i);
	if ( index != 0) {
		index->nuevoDocumento((char*)name);
	}
	(env)->ReleaseStringUTFChars(nombre,name);
}

/*
 * Class:     ar_uba_fi_taller2_ftrs_index_IndexadorImp
 * Method:    insertarTermino
 * Signature: (Ljava/lang/String;J)J
 */
JNIEXPORT jlong JNICALL Java_ar_uba_fi_taller2_ftrs_index_IndexadorImp_insertarTermino
  (JNIEnv *env, jobject obj, jstring nombre, jlong i) {
  	
	const char *name = env->GetStringUTFChars(nombre,0);
	Indexador *index = (Indexador*)JLongToPointer(i);
	jint ret = -1;
	if ( index != 0)
		ret = index->insertarTermino((char*)name);
	(env)->ReleaseStringUTFChars(nombre,name);
	return ret;
}

JNIEXPORT jintArray JNICALL Java_ar_uba_fi_taller2_ftrs_index_IndexadorImp_getDocs 
  (JNIEnv *env, jobject obj, jint offset, jint frec, jlong i) {

	Indexador *index = (Indexador*)JLongToPointer(i);
	tListaDocDistancias* listaDocFrec = index->getDocs(offset, frec);
	tListaDocDistancias::iterator it;

	cout << "tamaño del array: " << listaDocFrec->size() << endl;
	jintArray row = (jintArray)env->NewIntArray(listaDocFrec->size());
	jint* aux;
	int cont;

	cont = 0;
	if (listaDocFrec != NULL) {
		for(it=listaDocFrec->begin(); it!=listaDocFrec->end(); it++) {
			*aux = (*it)->getDocumento();
			cout << "en el for - *aux: " << *aux << " cont: " << cont << endl;
			env->SetIntArrayRegion((jintArray)row,(jsize)cont,1,aux);

			if (*it != NULL) delete *it;
			cont++;
		}
		delete listaDocFrec;
	}
	return row;
}
